/*
 * Temperature.m
 * This file is part of WX
 *
 * Copyright 2010-2013 Jason Coulls. All Rights Reserved.
 *
 * is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2 of the License.
 *
 * is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with . If not, see <http://www.gnu.org/licenses>.
 */

#import "Temperature.h"

@implementation Temperature
@synthesize reading;
@synthesize temperatureScale;

-(id) init
{
    self = [super init];
    //Create a default temperature instance.
    self.temperatureScale = kTEMPSCALECELSIUS;
    self.reading = 0;
    return self;
}

#pragma mark - Conversion Routines

-(double) celsiusValue
{
    double returnValue = 0;
    
    if (temperatureScale == kTEMPSCALECELSIUS) {
        //Already Celsius, so return current value.
        returnValue = reading;
        
    } else if (temperatureScale == kTEMPSCALEFAHRENHEIT) {
        //Convert Fahrenheit to Celsius
        returnValue =  (5/9) * reading - 32;
        
    } else if (temperatureScale == KTEMPSCALEKELVIN) {
        //Convert kelvin to Celsius
        returnValue =  reading - 273.15;
        
    }
    
    return returnValue;
}

-(double) fahrenheitValue
{
    double returnValue = 0;
    
    if (temperatureScale == kTEMPSCALECELSIUS) {
        returnValue =  (9/5) * reading + 32;
        
    } else if (temperatureScale == kTEMPSCALEFAHRENHEIT) {
        //Already Fahrenheit, so return current value.
        returnValue = reading;
        
    } else if (temperatureScale == KTEMPSCALEKELVIN) {
        //Convert kelvin to Fahrenheit
        returnValue = ((reading -273.15) * 1.8) +32;
    
    }
    
    return returnValue;
}


-(double) kelvinValue
{
    double returnValue = 0;
    
    if (temperatureScale == kTEMPSCALECELSIUS) {
        returnValue =  reading + 273.15;
        
    } else if (temperatureScale == kTEMPSCALEFAHRENHEIT) {
        //Convert Fahrenheit to kelvin.
        return ((reading + 459.67) * 5/9);
        
    } else if (temperatureScale == KTEMPSCALEKELVIN) {
        //Already kelvin, so return current value.
        returnValue = reading;
        
    }
    
    return returnValue;
 
}

@end
